package com.hit.client.driver.netty.router;

import com.hit.client.driver.netty.strategy.*;
import com.hit.client.driver.netty.strategy.GfMessageHandlerStrategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GfMessageRouter {
    // 路由表，存放命令和对应的handler
    private static final Map<String, GfMessageHandlerStrategy> strategyMap = new ConcurrentHashMap<>();

    static {
        // 初始化路由表，将命令和处理器绑定
        // 字母大写
        strategyMap.put(getHexKey((byte) 0x7C), new Gf7CStrategy());
        strategyMap.put(getHexKey((byte) 0x7D), new Gf7DStrategy());
        strategyMap.put(getHexKey((byte) 0x79), new Gf79Strategy());
        strategyMap.put(getHexKey((byte) 0x80), new Gf80Strategy());
    }

    // 根据命令获取处理器
    public static GfMessageHandlerStrategy getHandlerStrategy(byte mainCmd) {
        return strategyMap.get(getHexKey(mainCmd));
    }

    private static String getHexKey(Byte value) {
        // 转换为无符号整数，避免负数问题
        int unsignedByte = value & 0xFF;
        String hex = Integer.toHexString(unsignedByte).toUpperCase();  // 转换为大写的十六进制
        // 补齐到两位
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex;
    }
}
